package me.jessyan.mvparms.demo.mvp.ui.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.jess.arms.base.BaseHolder;
import com.jess.arms.base.DefaultAdapter;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.PerfectClickListener;

import java.util.ArrayList;

import me.jessyan.mvparms.demo.R;
import me.jessyan.mvparms.demo.bean.MainStore;

/**
 * Created by jianghejie on 15/11/26.
 */
public class StageredAdapter extends DefaultAdapter<MainStore> {

    private Context mContext;

    public StageredAdapter( Context mContext) {
        super(new ArrayList<>(), false);
        this.mContext = mContext;

    }

    public void setDatas(ArrayList<MainStore> list) {
        if (list != null&&list.size()>0) {
            mInfos.clear();
          mInfos.addAll(list);
            setImageScale();
        }

    }

    private void setImageScale() {
        for (final MainStore girlBean : mInfos) {
            if (girlBean.getScale() == 0) {
                Glide.with(mContext).load(girlBean.getUrl()).into(new SimpleTarget<Drawable>() {
                    @Override
                    public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                        float scale = resource.getIntrinsicWidth() / (float) resource.getIntrinsicHeight();
                        girlBean.setScale(scale);
                        notifyDataSetChanged();
                    }
                });
            } else {
                notifyDataSetChanged();
            }
        }
    }


    @Override
    public BaseHolder<MainStore> getHolder(View v, int viewType) {
        return new BaseHolder<MainStore>(v) {
            @Override
            public void setData(MainStore data, int position) {
                ImageView imageView = v.findViewById(R.id.main_store_pc_iv);

                imageView.setOnClickListener(new PerfectClickListener() {
                    @Override
                    protected void onNoDoubleClick(View v) {
                        if(mOnViewClickListener!=null){
                            mOnViewClickListener.onViewClick(v,position);
                        }
                    }
                });

                MainStore girlBean = mInfos.get(position);
                final ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams();
                layoutParams.width = ArmsUtils.getScreenWidth(mContext) / 2 - ArmsUtils.dip2px(mContext, 17);
                if (girlBean.getScale() != 0) {
                    layoutParams.height = (int) (layoutParams.width / girlBean.getScale());
                }
                Log.e("宽度", "更新" + layoutParams.width + " :高 " + layoutParams.height + " : " + girlBean.getScale());
//               imageView.setBackgroundColor(Color.BLUE);
                Glide.with(mContext)
                        .load(girlBean.getUrl())
                        .transition(new DrawableTransitionOptions().crossFade())
                        .into(imageView);
            }
        };
    }

    @Override
    public int getLayoutId(int viewType) {
        return R.layout.item_mian_store;
    }


}





















